# -*- coding: utf-8 -*-
# @Time : 2022/2/8 15:01
import json
from Common.mysql import app
from flask import jsonify, request
from src.User.token import token_verify, token_user_id, before_request
from Common.yaml_method import YamlMethod
from src.ApiTest.ProjectConfig.BaseInfo.add_base_info import AddBaseInfo
from src.ApiTest.ProjectConfig.BaseInfo.base_info_list import BaseList
from src.ApiTest.ProjectConfig.BaseInfo.update_base_info import UpdateBaseInfo
from src.ApiTest.ProjectConfig.BaseInfo.delete_base_info import DeleteBaseInfo

code = YamlMethod().read_data('code.yaml')['code']


@app.route('/url/add', methods=['POST'])
@token_verify
def add_base_info(create_user):
    """
    添加配置信息接口
    :param create_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    system_name = data['systemName']
    project_data = data['projectName'].split('/')
    project_name = project_data[0]
    project_id = project_data[1]
    base_url_test = data['base_url_test']
    base_url_develop = data['base_url_develop']
    base_url_release = data['base_url_release']
    header = data['header']

    # 校验参数
    if not all([system_name, project_id, project_name, base_url_test]):
        return jsonify(code=code[7], msg="信息不完整")

    # 提交注册信息
    res = AddBaseInfo().add_base_info(system_name, project_id, project_name, base_url_test, base_url_develop, base_url_release, header, create_user)
    return jsonify(res)


@app.route('/url/list', methods=['POST'])
@token_user_id
def base_info_list(user_id):
    """
    获取配置列表接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    page = data['page']
    limit = data['limit']
    system_name = data['systemName']
    project_data = data['projectId']
    if '/' in str(project_data):
        project_id = project_data.split('/')[1]
    else:
        project_id = project_data

    res = BaseList().base_list(page, limit, system_name, user_id, project_id)
    return res


@app.route('/url/update', methods=['POST'])
@token_verify
def base_info_update(update_user):
    """
    更新配置信息接口
    :param update_user: 操作用户名
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    system_id = data['id']
    system_name = data['systemName']
    project_data = data['projectName']
    if '/' in project_data:
        project_data = data['projectName'].split('/')
        project_name = project_data[0]
        project_id = project_data[1]
    else:
        project_name = project_data
        project_id = data['projectId']
    base_url_test = data['base_url_test']
    base_url_develop = data['base_url_develop']
    base_url_release = data['base_url_release']
    header = data['header']

    # 校验参数
    if not all([system_name, project_id, project_name, base_url_test, update_user]):
        return jsonify(code=code[7], msg="信息不完整")

    res = UpdateBaseInfo().update_base_info(system_id, system_name, project_id, project_name, base_url_test, base_url_develop, base_url_release, header, update_user)
    return res


@app.route('/url/delete', methods=['POST'])
@before_request
def base_info_delete():
    """
    删除配置信息接口
    :return:
    """
    data = json.loads(str(request.data, 'utf-8'))
    system_name = data['systemName']

    res = DeleteBaseInfo().delete_base_info(system_name)
    return res
